//
//  HD_NKM_LanguageVC+NetTip.m
//  HDLiaoNingMeseum
//
//  Created by HDNiuKuiming on 2017/8/3.
//  Copyright © 2017年 HDNiuKuiming. All rights reserved.
//

#import "HD_NKM_LanguageVC+NetTip.h"

@implementation HD_NKM_LanguageVC (NetTip)



#pragma mark
#pragma mark -------- Net Action --------

- (void)netStatusAction
{
    self.notification = [[AFDropdownNotification alloc] init];
    self.notification.notificationDelegate = self;
    NSString __block *netStr   = nil;
    HD_NKM_LanguageVC __block *weakSelf = self;
    
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        HDDeclare *declare = [HDDeclare sharedDeclare];
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                LOG(@"未识别的网络");
                netStr = @"未识别的网络";
                declare.netStatus = NO;
                declare.net_Status_Type = kNet_Status_Failed;
                [weakSelf showNotification:netStr];
                break;
                
            case AFNetworkReachabilityStatusNotReachable:
                LOG(@"不可达的网络(未连接)");
                netStr = @"不可达的网络(未连接)";
                declare.netStatus = NO;
                declare.net_Status_Type = kNet_Status_Failed;
                [self tipTextColor:[UIColor redColor]];
                [weakSelf showNotification:netStr];
                break;
                
            case AFNetworkReachabilityStatusReachableViaWWAN:
                LOG(@"3G,4G...的网络");
                netStr = @"3G,4G...的网络";
                declare.net_Status_Type = kNet_Status_3G_4G;
                declare.netStatus = YES;
                [self datasRequestAction];
                
                [weakSelf showNotification:netStr];
                break;
                
            case AFNetworkReachabilityStatusReachableViaWiFi:
                LOG(@"wifi的网络");
                netStr = @"wifi的网络";
                declare.netStatus = YES;
                declare.net_Status_Type = kNet_Status_Wifi;
                
                [self datasRequestAction];
                [weakSelf showNotification:netStr];
                break;
                
            default:
                break;
        }
    }];
    //3.开始监听
    [manager startMonitoring];
}

// 下载数据
- (void)datasRequestAction
{
//    NSString *deviceNum = [[NSUserDefaults standardUserDefaults] objectForKey:kFirst_Machine_ID];
//    
//    if (deviceNum.length==0) {
//        [self machineIDRequestAction];
//    } // 首次安装存在用户网络授权，所以要延迟加载一下

//    if([[[HDDeclare sharedDeclare] resArray] count]==0) {
//        [self performSelectorInBackground:@selector(allResRequestAction) withObject:nil]; // 请求全部数据
//    }
//    
//    if([[[HDDeclare sharedDeclare] mapPOIArray] count]==0) {
//        [self performSelectorInBackground:@selector(allMapPOIRequestAction) withObject:nil]; // 请求全部数据
//    }
//    
//    if ([[[HDDeclare sharedDeclare] mapSizeArray] count] == 0) {
//        [self performSelectorInBackground:@selector(allMapInfoRequestAction) withObject:nil];
//    }
}

-(IBAction)showNotification:(NSString *)subTitle {
    
    self.notification.titleText        = subTitle;
    self.notification.dismissOnTap     = YES;
    
    HDDeclare *declare = [HDDeclare sharedDeclare];
    [self.notification presentInView:self.view isConnected:declare.netStatus];
    
    [self.notification performSelector:@selector(dismissWithGravityAnimation:) withObject:[NSNumber numberWithBool:NO] afterDelay:2.0];
}

- (void)tipTextColor:(UIColor *)color
{
    [self.notification changeTextColor:color];
}

-(void)dropdownNotificationTopButtonTapped {
    
    NSLog(@"Top button tapped");
    [self.notification dismissWithGravityAnimation:NO];
}

-(void)dropdownNotificationBottomButtonTapped {
    
    NSLog(@"Bottom button tapped");
    [self.notification dismissWithGravityAnimation:NO];
}

#pragma mark
#pragma mark -------- Machine ID --------

- (void)machineIDRequestAction
{
    HDDeclare *declare = [HDDeclare sharedDeclare];
    NSString *requestStr = [NSString stringWithFormat:@"%@%@",[HDDeclare IP_Request_Header],HTTP_generateDeviceID];
    NSDictionary *pramaDic = @{@"app_kind":@"2",
                               @"language":[NSNumber numberWithInt:declare.languageType]};
    
    [HD_LY_NetHelper requestWithType:POST
                             withUrl:requestStr
                          withParams:pramaDic
                            toShowVC:nil
                             success:^(id response) {
                                 NSDictionary *dic = response;
                                 if ([dic isKindOfClass:[NSDictionary class]]) {
                                     NSNumber *status = dic[@"status"];
                                     if (status.integerValue == 1) {
                                         NSString *machineID = dic[@"data"];
                                         [[NSUserDefaults standardUserDefaults] setObject:machineID forKey:kFirst_Machine_ID];
                                     }
                                     else {
                                         LOG(@"Machine erro, response is %@", response);
                                     }
                                 }
                             }
                                fail:^(NSError *error) {
                                    LOG(@"machine error：%@", error);
                                }
                             showHUD:NO
                             isCache:NO];
}

@end
